#ifndef PCAPIO_H
#define PCAPIO_H
#include <QString>
#include <QMap>
#include <QVector>
#include <QString>
#include <QAtomicInt>
#include <atomic>
#include <QDateTime>
#include <QMutex>
#define PCAPIO_BUFCNT 65536
#define PCAPIO_MAXPACK 10000
#define CAP_FADE_MSECS 5000
namespace PCAPIO{

	struct tag_packages{
		int from_id;//From which port
		int to_id;//To which port
		int len;
		QByteArray data;
	};

	struct tag_portAssign{
		int curr_id;
		quint64 mac;
		QString portName;
		QDateTime dtmLastAck;
	};

	extern QVector<tag_packages> global_buffer;
	extern QAtomicInteger<quint64> pcap_recv_pos;
	extern std::atomic<bool> pcap_stop;
	extern QMap<quint64,tag_portAssign> pcap_ports;
	extern QMutex mtx_ports;

	//Functions

	//0. Enum Devices
	QString pcapio_interfaces(QMap<QString,QString> & devmap);
	//1. Initial Buffer
	void init_buffer();
	//2. reset for start
	void reset();
	//3. Run Cap Thread on interface.
	void recv_loop(QString itstr, QString filter, int id, int tcp_exlude, std::function<void (QString) > msg);
	//3.1 enqueue
	bool enqueuePack(const  char * pack, const int len, const int id, const QString & portname );
	//4. Run Send Thread on interface
	void send_loop(QString itstr,int id,bool bSwitchMod, std::function<void (QString) > msg);
	//5. Clients
	QMap<quint64,tag_portAssign> macPorts( );

}

#ifndef _MSC_VER
	inline int memcpy_s(void * dest, size_t dstSz, const void * src, size_t srcSize)
	{
		if (dstSz < srcSize)
			return 22;
		memcpy(dest,src,srcSize);
		return 0;
	}
#endif

#endif // PCAPIO_H
